package com.example.cf.grouplist.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.cf.grouplist.Bean.ClassBean;
import com.example.cf.grouplist.R;

import java.util.List;

/**
 * Created by cf on 2016/8/11.
 */

public class BaseRecyclerAdapter extends RecyclerView.Adapter<BaseRecyclerAdapter.BaseHolder> {
    private Context context;

    public ItemOnclickListener getItemOnclickListener() {
        return itemOnclickListener;
    }

    public void setItemOnclickListener(ItemOnclickListener itemOnclickListener) {
        this.itemOnclickListener = itemOnclickListener;
    }

    private ItemOnclickListener itemOnclickListener;

    public void setItemOnLongClickListener(ItemOnLongClickListener itemOnLongClickListener) {
        this.itemOnLongClickListener = itemOnLongClickListener;
    }

    private ItemOnLongClickListener itemOnLongClickListener ;
    public BaseRecyclerAdapter(Context context, List<ClassBean> data) {
        this.context = context;
        this.data = data;
    }

    private List<ClassBean> data;

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseHolder baseHolder = new BaseHolder(LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false));
        return baseHolder;
    }

    @Override
    public void onBindViewHolder(final BaseHolder holder, final int position) {
        if (position % 5 == 0) {
            holder.class_tv.setVisibility(View.VISIBLE);
            holder.class_tv.setText(position/5+"");
            holder.itemView.setTag(position/5+"");
        }else{
            holder.class_tv.setVisibility(View.GONE);
            holder.itemView.setTag(null);
        }
        holder.itemView.setContentDescription(position/5+"");
        holder.title_tv.setText(data.get(position).getClassName());
        if (itemOnclickListener != null)
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemOnclickListener.itemOnclick(holder, holder.itemView, position);
                }
            });
        if (itemOnLongClickListener!=null)
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    itemOnLongClickListener.itemOnLongclick(holder,holder.itemView,position);
                    return true;
                }
            });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    /**
     * Viewholder
     */
    public static class BaseHolder extends RecyclerView.ViewHolder {

        private final ImageView head_iv;
        private TextView title_tv;
        private TextView content_tv;
        private View itemView;
        private TextView class_tv;

        public BaseHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            title_tv = (TextView) itemView.findViewById(R.id.title_tv);
            content_tv = (TextView) itemView.findViewById(R.id.content_tv);
            head_iv = ((ImageView) itemView.findViewById(R.id.head_iv));
            class_tv = ((TextView) itemView.findViewById(R.id.class_tv));
        }
    }

    public static interface ItemOnclickListener {
        void itemOnclick(BaseHolder parent, View item, int pos);
    }
    public static interface ItemOnLongClickListener{
        void itemOnLongclick(BaseHolder parent, View item, int pos);
    }

}
